$(function () {
    $("#jqGrid").jqGrid({
        url:baseURL+"sys/terminalDue/list" ,
        datatype: "json",
        colModel: [
            { label: 'id', name: 'id', width: 80,align:'center' , sortable: false},
            { label: 'SN', name: 'sn', width: 80,align:'center' , sortable: false},
            { label: '激活状态', name: 'isActivation', width:45, formatter:function(value, options, row){
                return value=='1'?"未激活":(value=='2'?"已激活":"禁用")},align:'center' , sortable: false},
            { label: '激活时间', name: 'activationDate', width:75, formatter:function(value, options, row){
                    return value==null?"-":value;},align:'center' , sortable: false},
            { label: '返现期限', name: 'dueDate', width:75, formatter:function(value, options, row){
                    return value==null?"-":value;},align:'center' , sortable: false},
            { label: '返现金额', name: 'currentCash', width: 60,align:'center',formatter:function (value,options,row) {
                    return value==null?"0.00":value;
                } , sortable: false},
            { label: '逾限金额', name: 'afterCash', width: 60,align:'center',formatter:function (value,options,row) {
                    return value==null?"0.00":value;
                } , sortable: false},
            { label: '返现人姓名', name: 'purName', width: 75,align:'center',formatter:function (value,options,row) {
                    return value==null?"-":value;
                } , sortable: false},
            { label: '返现人手机', name: 'purPhone', width: 75,align:'center',formatter:function (value,options,row) {
                    return value==null?"-":value;
                } , sortable: false},
            { label: '当前归属', name: 'ownName', width: 75,align:'center',formatter:function (value,options,row) {
                    return value==null?"-":value;
                } , sortable: false},
            { label: '交易总额', name: 'amount', width: 75,align:'center',sortable: false},
        ],
		viewrecords: true,
        height: 375,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect:true,
        multiboxonly:true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });

    bindType();

});




function toUpdate(terminalId){
    $.ajax({
        url:baseURL+"sys/terminalInfo/queryOne",
        data:{"terminalId":terminalId},
        dateType:"json",
        type:"post",
        success:function (r) {
                vm.terminal=r.terminal;
                console.log(vm.terminal)
        }
        
        
    })
}


var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            startSn:null,
            endSn:null,
            merchantCode:null,
            activation:0,
            terminalType:0,
            ownName:null
        },
        activations:[
            {
                value:0,
                name:"请选择激活状态"
            },
            {
                value:1,
                name:"未激活"
            },
            {
                value:2,
                name:"已激活"
            },
            {
                value:3,
                name:"禁用"
            }
        ],
        terminalTypes:[],
        showList: true,
        showUpdate:false,
        title:null,
        activDue:{
            snStr:null,
            currentCash:null,
            dueDate:null,
            afterCash:null,
            model:null,
            idList:[]
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        clean: function () {
            vm.q = {
                startSn:null,
                endSn:null,
                merchantCode:null,
                activation:0,
                terminalType:0
            }
            $("#jqGrid").jqGrid('setGridParam', {page: 1})
            vm.reload();
        },
        reload: function () {
            vm.showList = true;
            vm.showUpdate=false;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');

            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'merchantCode': vm.q.merchantCode,
                    'terminalType': vm.q.terminalType,
                    'startSn': vm.q.startSn,
                    'endSn':vm.q.endSn,
                    "activation": vm.q.activation,
                    'ownName':vm.q.ownName
                },
                page: page
            }).trigger("reloadGrid");
            bindType();
        },
        update:function () {
            vm.showList = false;
            vm.showUpdate=true;
            let rows=getSelectedRows();

            if(rows==null || rows.length==0){
                return;
            }
            let idList=new Array();
            for(let i=0;i<rows.length;i++){
                let rowid=rows[i];
                let row =$("#jqGrid").jqGrid("getRowData",rowid)
                idList.push(row.id);
            }
            $.ajax({
                url:baseURL+"sys/terminalDue/getInfoByIdlist",
                data:{"idList":idList},
                dataType:"json",
                type:"post",
                traditional:true,
                success:function (r) {
                    console.log(r)
                    vm.activDue=r.activDue;
                }
            })
            

        },
        modify:function () {
            $.ajax({
                url:baseURL+"sys/terminalDue/updateByIdList",
                contentType: 'application/json',
                type:"post",
                data:JSON.stringify(vm.activDue),
                success:function (r) {
                    if(r.code==0){
                        layer.msg("成功");
                    }else {
                        layer.msg(r.msg);
                    }
                }



            })
        }
    }
});



Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

function bindType() {

        $.getJSON(baseURL + "sys/terminalType/list", function (r) {
            vm.$set(vm.terminalTypes, 0, {
                terminalType: 0,
                terminalName: "请选择类型"
            });
            for (var i = 0; i < r.page.length; i++) {
                vm.$set(vm.terminalTypes, (i + 1), r.page[i]);
            }

        })
}